package com.ruoyi.web.controller.tool.dataManage;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataSetUtil {

    /**
     * 通过根节点主键 和 外部传入参数获取数据
     * @param id
     * @param parameter
     * @return
     */
    public static Map<String,Object> getDataSetRoot(String id,Map<String,Object> parameter)
    {
        Map<String,Object> returnMap=new HashMap<>();
        Record dataSet= Db.findFirst("select * from sys_data_set where id=?",id);
        if(dataSet!=null)
        {
            String setName=dataSet.getStr("name");
            String setCode=dataSet.getStr("code");
            String setType=dataSet.getStr("type");
            String setSQl=dataSet.getStr("SQL");
//          简单验证入参是否完全
            List<Record> parameList=Db.find("select * from sys_data_set_parameter where set_id=? order by sort",id);
            boolean v=true;
            for(Record parame:parameList)
            {
                String parameCode=parame.getStr("code");
                if(parameter.get(parameCode)==null)
                {
                    v=false;
                    break;
                }
            }
            if(!v)
            {
                returnMap.put("code","500");
                returnMap.put("msg","无法从父级获取"+setName+"["+setCode+"]数据集参数，请检查配置文件！");
                return returnMap;
            }
//          开始按照入参取数
            if(setType.equals("object"))
            {

                SqlPara sp=new SqlPara();
                sp.setSql(setSQl);

                for(Record data:parameList)
                {
                    sp.addPara(parameter.get(data.getStr("code")));
                }
                Record rentunData=Db.findFirst(sp);
                Map<String,Object> obj=new HashMap<>();
                if(rentunData!=null)
                {
                    obj=rentunData.getColumns();
                }

                List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
                for(Record children:childrenList)
                {
                    Map<String,Object> childrenMap=getDataSetChildren(rentunData,children);
                    if(childrenMap.get("code").equals("200"))
                    {
                        obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                    }
                    else
                    {
                        obj.put(children.getStr("code"),childrenMap.get("msg"));
                    }
                }
                returnMap.put(setCode,obj);
                returnMap.put("code","200");
                returnMap.put("msg","获取数据集成功！");

            }
            if(setType.equals("array"))
            {
                try
                {
                    SqlPara sp=new SqlPara();
                    sp.setSql(setSQl);
                    for(Record data:parameList)
                    {
                        sp.addPara(parameter.get(data.getStr("code")));
                    }
                    List<Record> rentunList=Db.find(sp);
                    List<Map<String,Object>> dataListMap=new ArrayList<>();
                    for(Record rc:rentunList)
                    {
                        Map<String,Object> obj=rc.getColumns();
                        List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
                        for(Record children:childrenList)
                        {
                            Map<String,Object> childrenMap=getDataSetChildren(rc,children);
                            if(childrenMap.get("code").equals("200"))
                            {
                                obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                            }
                            else
                            {
                                obj.put(children.getStr("code"),childrenMap.get("msg"));
                            }
                        }
                        dataListMap.add(obj);
                    }
                    returnMap.put(setCode,dataListMap);
                    returnMap.put("code","200");
                    returnMap.put("msg","获取数据集成功！");
                }
                catch(Exception e)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","获取数据集时执行SQL失败请检查相关配置！");
                }
            }
        }
        else
        {
            returnMap.put("code","500");
            returnMap.put("msg","查询不到数据集信息！");
        }
        return returnMap;
    }


    /**
     * 通过根节点ID和配置参数中的默认值获取测试数据
     * @param id 数据集主键
     * @return
     */
    public static Map<String,Object> getDataSetRoot(String id)
    {
        Map<String,Object> returnMap=new HashMap<>();
        Record dataSet= Db.findFirst("select * from sys_data_set where id=?",id);
        if(dataSet!=null)
        {
            String setCode=dataSet.getStr("code");
            String setType=dataSet.getStr("type");
            String setSQl=dataSet.getStr("SQL");
            if(setType.equals("object"))
            {
                try
                {
                    SqlPara sp=new SqlPara();
                    sp.setSql(setSQl);
                    List<Record> parameList=Db.find("select * from sys_data_set_parameter where set_id=? order by sort",id);
                    for(Record data:parameList)
                    {
                        sp.addPara(data.get("default"));
                    }
                    Record rentunData=Db.use("peisSystem").findFirst(sp);
                    Map<String,Object> obj=new HashMap<>();
                    if(rentunData!=null)
                    {
                        obj=rentunData.getColumns();
                    }
                    List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
                    for(Record children:childrenList)
                    {
                        Map<String,Object> childrenMap=getDataSetChildren(rentunData,children);
                        if(childrenMap.get("code").equals("200"))
                        {
                            obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                        }
                        else
                        {
                            obj.put(children.getStr("code"),childrenMap.get("msg"));
                        }
                    }
                    returnMap.put(setCode,obj);
                    returnMap.put("code","200");
                    returnMap.put("msg","获取数据集成功！");

                }
                catch(Exception e)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","获取数据集时执行SQL失败请检查相关配置！");
                }

            }
            if(setType.equals("array"))
            {
                try
                {
                    SqlPara sp=new SqlPara();
                    sp.setSql(setSQl);
                    List<Record> parameList=Db.find("select * from sys_data_set_parameter where set_id=? and status='0' order by sort",id);
                    for(Record data:parameList)
                    {
                        sp.addPara(data.get("default"));
                    }
                    List<Record> rentunList=Db.use("peisSystem").find(sp);
                    List<Map<String,Object>> dataListMap=new ArrayList<>();
                    for(Record rc:rentunList)
                    {
                        Map<String,Object> obj=rc.getColumns();
                        List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
                        for(Record children:childrenList)
                        {
                            Map<String,Object> childrenMap=getDataSetChildren(rc,children);
                            if(childrenMap.get("code").equals("200"))
                            {
                                obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                            }
                            else
                            {
                                obj.put(children.getStr("code"),childrenMap.get("msg"));
                            }
                        }
                        dataListMap.add(obj);
                    }
                    returnMap.put(setCode,dataListMap);
                    returnMap.put("code","200");
                    returnMap.put("msg","获取数据集成功！");
                }
                catch(Exception e)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","获取数据集时执行SQL失败请检查相关配置！");
                }
            }
        }
        else
        {
            returnMap.put("code","500");
            returnMap.put("msg","查询不到数据集信息！");
        }
        return returnMap;
    }


    public static Map<String,Object> getDataSetChildren(Record parentData,Record childrenSet)
    {
        Map<String,Object> returnMap=new HashMap<>();

        String id=childrenSet.getStr("id");
        String setName=childrenSet.getStr("name");
        String setCode=childrenSet.getStr("code");
        String setType=childrenSet.getStr("type");
        String setSQl=childrenSet.getStr("SQL");

        if(parentData==null)
        {
            Map<String,Object> obj=new HashMap<>();
            List<Map<String,Object>> dataListMap=new ArrayList<>();

            List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
            for(Record children:childrenList)
            {
                Map<String,Object> childrenMap=getDataSetChildren(null,children);
                if(childrenMap.get("code").equals("200"))
                {
                    obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                }
                else
                {
                    obj.put(children.getStr("code"),childrenMap.get("msg"));
                }
            }
            if(setType.equals("object"))
            {
                returnMap.put(setCode,obj);
            }
            if(setType.equals("array")) {
                returnMap.put(setCode,dataListMap);
            }

            returnMap.put("code","200");
            returnMap.put("msg","获取数据集成功！");
        }
        else
        {
            if(setType.equals("object"))
            {
                List<Record> parameList=Db.find("select * from sys_data_set_parameter where set_id=? order by sort",id);
                boolean v=true;
                for(Record parame:parameList)
                {
                    String parameCode=parame.getStr("code");
                    if(parentData.get(parameCode)==null)
                    {
                        v=false;
                        break;
                    }
                }
                if(!v)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","无法从父级获取"+setName+"["+setCode+"]数据集参数，请检查配置文件！");
                    return returnMap;
                }
                try
                {
                    SqlPara sp=new SqlPara();
                    sp.setSql(setSQl);
                    for(Record parame:parameList)
                    {
                        String parameCode=parame.getStr("code");
                        sp.addPara(parentData.get(parameCode));
                    }
                    Record rentunData=Db.use("peisSystem").findFirst(sp);
                    Map<String,Object> obj=new HashMap<>();
                    if(rentunData!=null)
                    {
                        obj=rentunData.getColumns();
                    }
                    List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
                    for(Record children:childrenList)
                    {
                        Map<String,Object> childrenMap=getDataSetChildren(rentunData,children);
                        if(childrenMap.get("code").equals("200"))
                        {
                            obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                        }
                        else
                        {
                            obj.put(children.getStr("code"),childrenMap.get("msg"));
                        }
                    }
                    returnMap.put(setCode,obj);
                    returnMap.put("code","200");
                    returnMap.put("msg","获取数据集成功！");
                }
                catch(Exception e)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","获取"+setName+"["+setCode+"]数据集时执行SQL失败请检查相关配置！");
                }
            }
            if(setType.equals("array")) {

                List<Record> parameList=Db.find("select * from sys_data_set_parameter where set_id=? order by sort",id);
                boolean v=true;
                for(Record parame:parameList)
                {
                    String parameCode=parame.getStr("code");
                    if(parentData.get(parameCode)==null)
                    {
                        v=false;
                        break;
                    }
                }
                if(!v)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","无法从父级获取"+setName+"["+setCode+"]数据集参数，请检查配置文件！");
                    return returnMap;
                }
                try
                {
                    SqlPara sp=new SqlPara();
                    sp.setSql(setSQl);
                    for(Record parame:parameList)
                    {
                        String parameCode=parame.getStr("code");
                        sp.addPara(parentData.get(parameCode));
                    }
                    List<Record> rentunList=Db.use("peisSystem").find(sp);
                    List<Map<String,Object>> dataListMap=new ArrayList<>();
                    for(Record rc:rentunList)
                    {
                        Map<String,Object> obj=rc.getColumns();
                        List<Record> childrenList=Db.find("select * from sys_data_set where pid=?",id);
                        for(Record children:childrenList)
                        {
                            Map<String,Object> childrenMap=getDataSetChildren(rc,children);
                            if(childrenMap.get("code").equals("200"))
                            {
                                obj.put(children.getStr("code"),childrenMap.get(children.getStr("code")));
                            }
                            else
                            {
                                obj.put(children.getStr("code"),childrenMap.get("msg"));
                            }

                        }

                        dataListMap.add(obj);
                    }
                    returnMap.put(setCode,dataListMap);
                    returnMap.put("code","200");
                    returnMap.put("msg","获取数据集成功！");
                }
                catch(Exception e)
                {
                    returnMap.put("code","500");
                    returnMap.put("msg","获取"+setName+"["+setCode+"]数据集时执行SQL失败请检查相关配置！");
                }

            }
        }
        return returnMap;
    }
}
